#include <stdio.h>
#include "ofc/DHashTable.h"
#include "ofc/DText.h"
#include "ofc/DInt.h"

int main(int argc, char *argv[])
{
  DHashTable    *tbl  = [DHashTable    alloc];
  DHashIterator *iter = [DHashIterator new  ];
  DText         *str  = [DText         new  ];
  DText         *key;
  DInt          *nr;

  [tbl init :[DText class]];           // Init table with the type of the key: DText

  [str set :"Key1"];
  nr  = [DInt  alloc]; [nr  init :7];
  [tbl insert :str :nr];               // Insert in table: Key1,7

  [str set :"Key2"];
  nr  = [DInt  alloc]; [nr  init :5];
  [tbl insert :str :nr];               // Insert in table: Key2,5

  [str set :"Key3"];
  nr  = [DInt  alloc]; [nr  init :9];
  [tbl insert :str :nr];               // Insert in table: Key3,9

  [str set :"Key4"];
  nr  = [DInt  alloc]; [nr  init :1];
  [tbl insert :str :nr];               // Insert in table: Key4,1

  [str set :"Key5"];
  nr  = [DInt  alloc]; [nr  init :6];
  [tbl insert :str :nr];               // Insert in table: Key5,6

  printf("Number of elements in the hashtable:%ld.\n", [tbl length]);

  [str set :"Key6"];                   // Check for element in hashtable
  printf("%s %s present in the hashtable.\n", [str cstring], ([tbl has :str] ? "is" : "is not"));

  [str set :"Key2"];
  nr = [tbl get :str];                 // Get element from hashtable
  printf("%s has value %d in the hash table.\n", [str cstring], [nr get]);

  [str set :"Key3"];
  nr = [tbl delete :str];              // Delete element from hashtable
  if (nr != nil)
  {
    printf("%s is removed from the hashtable.\n", [str cstring]);
    [nr free];
  }
  else
    printf("%s could not be found in the hashtable.\n", [str cstring]);

                                       // Use the iterator to iterate the table
  [iter hashTable :tbl];               // Set the hashtable in the iterator
  nr = [iter first];                   // Get the first element
  while (nr != nil)
  {
    key = [iter key];                  // Get the current key
    printf("Element [%s,%d].\n", [key cstring], [nr get]);
    nr = [iter next];                  // Get the next element
  }

  [iter free];                         // Cleanup
  [tbl  free];

  return 0;
}

